package com.wuhongyu.util;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

import org.springframework.web.multipart.MultipartFile;

import com.wuhongyu.common.MyCommon;

public class MyUtil {
	// MD5加密
	public static String encryptMD5(String str) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] byteArray = md.digest(str.getBytes());
			BigInteger bi = new BigInteger(1, byteArray);
			return bi.toString(16);
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException();
		}
	}

	// 分页
	public static Integer getCurrentPage(Integer currentPage) {
		if (currentPage == null || currentPage <= 0) {
			currentPage = 1;
		}
		return currentPage;
	}

	// 生成一个永不重复的图片名
	public static String getNewFileName(String photoFileName) {
		String str = UUID.randomUUID().toString();
		str = str.replaceAll("-", "");
		if (photoFileName != null && photoFileName.indexOf(".") > 0) {
			String picExt = photoFileName.substring(photoFileName.lastIndexOf("."));
			str += picExt;
		}
		return str;
	}

	public static void insertSchedule(MultipartFile schedule, String fileName) {
		File newFile = new File(MyCommon.FILEPATH + fileName);
		try {
			schedule.transferTo(newFile);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void deleteSchedule(String fileName) {
		File file = new File(MyCommon.FILEPATH, fileName);
		file.delete();
	}
}
